package com.example.jiangkt.ergongyi.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.jiangkt.ergongyi.R;

/**
 * Created by jiangkt on 2018/3/13.
 */

public class ScratchSurFaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    private SurfaceHolder mHolder;
    private Path mPath;
    private Paint mPaint;

    private Bitmap bgBitmapBottom;
    private Bitmap bgBitmapTop;
    private Bitmap cacheBitmapBottom;
    private Bitmap cacheBitmapTop;

    private Canvas mCanvas;
    private Canvas cacheCanvasBottom;
    private Canvas cacheCanvasTop;
    private boolean mIsDrawing;
    private int randomCard;

    private int x, y, viewWidth, viewHeight, bgBitmapWidth, bgBitmapHeight, left, top;
    private float scale = 1.0f;
    private Matrix matrix;

    public int getRandomCard() {
        return randomCard;
    }

    public void setRandomCard(int randomCard) {
        this.randomCard = randomCard;
    }

    private void initView() {
        mHolder = getHolder();
        mHolder.addCallback(this);

        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);

        mPath = new Path();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(100);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    }

    //背景图片在手机中位置的布局
    private void backgroundBitmapLayout() {
        //最版Android studio不能获取手机屏幕宽度和高度，不知道为什么
        //viewWidth = getWidth(); //手机屏幕宽度
        //viewHeight = getHeight(); //手机屏幕高度
        //暂时将宽度和高度写死
        viewWidth = 240;
        viewHeight = 360;

        bgBitmapWidth = bgBitmapBottom.getWidth(); //背景图片宽度
        bgBitmapHeight = bgBitmapBottom.getHeight(); //背景图片高度

        matrix = new Matrix();
        scale = 1.0f;

        scale = (float) (viewWidth / (float) bgBitmapWidth);
        left = 0;
        top = 0;

        matrix.postScale(scale, scale);
        matrix.postTranslate(left, top);
    }

    private void initBitmap() {
        bgBitmapBottom = BitmapFactory.decodeResource(getResources(),R.drawable.bg);
        bgBitmapTop = BitmapFactory.decodeResource(getResources(), R.drawable.block_3);

        this.backgroundBitmapLayout();

        cacheBitmapBottom = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
        cacheBitmapTop = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);

        cacheCanvasBottom = new Canvas(cacheBitmapBottom);
        cacheCanvasTop = new Canvas(cacheBitmapTop);

        cacheCanvasBottom.drawColor(Color.WHITE);//背景图片底部颜色 透明，为了显示整个视图底部背景颜色
        cacheCanvasBottom.drawBitmap(bgBitmapBottom, matrix, null);//再生成背景图片

        cacheCanvasTop.drawColor(Color.TRANSPARENT);//整个视图底部背景颜色 白色，
        cacheCanvasTop.drawBitmap(bgBitmapTop, matrix, null);
    }

    //该绘画方法，使得 背景图片 位图 位于 面向用户 第一层，画笔不能覆盖该背景图片，所以找该绘画游戏的背景图片的背景不能是白色背景，只能图片背景是透明的
    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();

            //锁住位图
            mCanvas.drawBitmap(cacheBitmapBottom, 0, 0, null); //锁住的第二个位图  所以该位图在第一个位图的上面 第一层
            mCanvas.drawBitmap(cacheBitmapTop, 0, 0, null); //锁住的第一个位图  所以该位图在手机底部 第二层

            cacheCanvasTop.drawPath(mPath, mPaint); //画在锁住的第一个位图上

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }

    public void licensing() {
        if (bgBitmapBottom != null && !bgBitmapBottom.isRecycled()) {
            bgBitmapBottom.recycle();
        }
        initView();
        initBitmap();
        draw();
    }

    public ScratchSurFaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mIsDrawing = true;
        this.initBitmap();
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mIsDrawing = false;
    }

    @Override
    public void run() {
        while (mIsDrawing) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (mHolder) {
                draw();
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int) event.getX();
        y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x, y);
                break;
            default:
                break;
        }
        return true;
    }
}
